package com.gviiii.pprf.netty;

import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("msgTagCacheService")
//@CacheConfig(cacheNames = {"MsgTagCache"})
public class MsgTagCacheService {

    @Resource
    private CacheManager cacheCacheManager;

    public void setMsgTag(Integer msgTagCode) {
        Cache msgTagCache = cacheCacheManager.getCache("MsgTagCache");
        if (msgTagCache != null) {
            msgTagCache.put(RfChannelHandler.MSG_TAG, msgTagCode);
        }
    }

    public Integer getMsgTagCode() {
        Cache msgTagCache = cacheCacheManager.getCache("MsgTagCache");
        if (msgTagCache != null) {
            Cache.ValueWrapper valueWrapper = msgTagCache.get(RfChannelHandler.MSG_TAG);
            if (valueWrapper != null) {
                return Integer.valueOf(String.valueOf(valueWrapper.get()));
            }
        }
        return null;
    }

    public void removeMsgTag() {
        Cache msgTagCache = cacheCacheManager.getCache("MsgTagCache");
        if (msgTagCache != null) {
            msgTagCache.evict(RfChannelHandler.MSG_TAG);
        }
    }
}
